package com.yz.food.cms.controller;

import com.yz.food.cms.common.ServerResponse;
import com.yz.food.cms.pojo.Recipes;
import com.yz.food.cms.service.RecipesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RecipesController {
    @Autowired
    private RecipesService recipesService;


    /**
     * 跳转到食谱界面
     */
    @RequestMapping("page/recipes")
    public String recipesPage(ModelMap modelMap) {
        List<Recipes> recipes = recipesService.getAll();
        modelMap.addAttribute("recipes", recipes);
        return "recipes";
    }



    /**
     * 更新食谱
     */
    @RequestMapping("recipes/updateRecipes")
    @ResponseBody
    public ServerResponse updateRecipes(Recipes recipes) {
        int result = recipesService.updateRecipes(recipes);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("成功");
        }
        return ServerResponse.createByErrorMessage("失败");
    }

    /**
     * 删除食谱
     */
    @RequestMapping("recipes/deleteById")
    @ResponseBody
    public ServerResponse deleteById(@RequestParam("recipesId") Integer recipesId) {
        int result = recipesService.deleteById(recipesId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("成功");
        }
        return ServerResponse.createByErrorMessage("失败");
    }
}
